package com.app.actions.admin;

import com.app.SpringAnnotationDef;
import com.app.actions.BaseAction;
import com.app.model.ORG_GorupManager;
import com.app.model.ORG_GorupResources;
import com.app.model.ORG_Resources;
import com.app.model.User;
import com.app.service.admin.AdminService;
import com.app.service.admin.GorupManagerService;
import net.sf.json.JSONObject;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: cnprinces
 * Date: 13-1-21
 * Time: 下午4:35
 * To change this template use File | Settings | File Templates.
 */
@Scope(SpringAnnotationDef.SCOPE_PROTOTYPE)
@Component
@Namespace("/admin")
public class GorupManagerAction extends BaseAction {
    @Resource(name = SpringAnnotationDef.GORUPMANAGER_SERVICE)
    private GorupManagerService service;


    @Resource(name = SpringAnnotationDef.ADMIN_SERVICE)
    private AdminService resService;


    private ORG_GorupManager gorupManager;

    private String[] resList;

    public String[] getResList() {
        return resList;
    }

    public void setResList(String[] resList) {
        this.resList = resList;
    }

    public ORG_GorupManager getGorupManager() {
        return gorupManager;
    }

    public void setGorupManager(ORG_GorupManager gorupManager) {
        this.gorupManager = gorupManager;
    }

    //actions

    @Action(value = "gorupManager", results = {@Result(name = SUCCESS, location = "/admin/gorupManager/list.jsp"),
            @Result(name = "addGorupManager", location = "/admin/gorupManager/addGorupManager.jsp"),
            @Result(name = "cfgResources", location = "/admin/gorupManager/cfgResources.jsp")
    })
    public String getGorupManagerList() {
        List<ORG_GorupManager> list = service.getDao().listAll();
        getRequest().setAttribute("list", list);
        return SUCCESS;
    }


    public String toAdd() {

        if (gorupManager != null && gorupManager.getId() != null) {
            gorupManager = service.getDao().get(gorupManager.getId());
            getRequest().setAttribute("gorupManager", gorupManager);
        }
        return "addGorupManager";
    }


    public void addGorupManager() {
        gorupManager.setCreateDate(new Date());
        service.getDao().saveObject(gorupManager);


        JsonOut("添加成功!", "gorupManagerNav");

    }

    public void delGorupManager() {
        //先删除该用户组旗下的所有菜单

        service.getDao().deleteGorupResources(gorupManager.getId());
        //再删除该菜单
        service.getDao().delete(gorupManager.getId());
        JsonOut("删除成功!", "gorupManagerNav");

    }

    public String cfgResources() {
        //获得所有的资源菜单
        List<ORG_Resources> list = resService.getDao().listAll();

        //往request里放attribute
        getRequest().setAttribute("list", list);

        //取得父节点
        List<ORG_Resources> parentRes = new ArrayList<ORG_Resources>();
        //取得子节点
        List<ORG_Resources> sonRes = new ArrayList<ORG_Resources>();
        for (ORG_Resources res : list) {
            if (res.getParentId() == 0) {
                parentRes.add(res);
            } else {
                sonRes.add(res);
            }
        }

        //取得用户组对象
        gorupManager = service.getDao().get(gorupManager.getId());

        //取得用户组已有权限列表
        List<ORG_GorupResources> resList = service.getDao().getGorupResources(gorupManager.getId());

        //取得该用户组旗下的员工列表
        List<User> userList = service.getDao().getUserByGorup(gorupManager.getId());

        getRequest().setAttribute("gorupManager", gorupManager);
        getRequest().setAttribute("userList", userList);
        getRequest().setAttribute("resList", resList);
        getRequest().setAttribute("parentRes", parentRes);
        getRequest().setAttribute("sonRes", sonRes);
        return "cfgResources";
    }


    public void editResources() {

        //1.先删除该用户组旗下的所有菜单

        if (resList != null && resList.length > 0) {
            service.getDao().deleteGorupResources(gorupManager.getId());
        }

        //2.循环针对当前菜单权限
        for (String str : resList) {
            ORG_GorupResources gRes = new ORG_GorupResources();
            gRes.setCreateDate(new Date());
            gRes.setResourcesId(Integer.parseInt(str));
            gRes.setGorupManagerId(gorupManager.getId());

            //保存对象
            service.getDao().saveObject(gRes);
        }

        JSONObject object = new JSONObject();


        object.put("statusCode", "200");
        object.put("message", "修改成功");
        object.put("navTabId", "");
        object.put("rel", "");
        object.put("callbackType", "");
        object.put("forwardUrl", "");
        out(object);
    }

}
